package edu.sysu.demo.petstore.token;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.security.PrivateKey;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class JWTIssuer implements Issuer {

  @Override
  public String issueToken(String subject, LocalDateTime expiration, PrivateKey privateKey) {

    Instant instant = expiration.atZone(ZoneId.systemDefault()).toInstant();

    return Jwts.builder()
        .setSubject(subject)
        .setIssuedAt(new Date())
        .setExpiration(Date.from(instant))
        .setAudience("pet-store")
        .signWith(SignatureAlgorithm.RS512, privateKey)
        .compact();

  }
}
